﻿using System;
using System.Text.RegularExpressions;
using System.Web.DynamicData;
using System.Web.UI;

namespace PD.Web.DynamicData.FieldTemplates
{
    /// <summary>
    /// Represents the enumeration field template control.
    /// </summary>
    public partial class EnumerationField : System.Web.DynamicData.FieldTemplateUserControl
    {
        /// <summary>
        /// Gets the data control that handles the data field in a field template.
        /// </summary>
        /// <returns>A data control that handles the data field in a field template.</returns>
        public override Control DataControl
        {
            get
            {
                return this.Literal1;
            }
        }

        /// <summary>
        /// Gets the enum field value string.
        /// </summary>
        public string EnumFieldValueString
        {
            get
            {
                if (this.FieldValue == null)
                {
                    return Regex.Replace(this.FieldValueString, "(\\B[A-Z])", " $1");
                }

                Type enumType = this.Column.GetEnumType();
                if (enumType != null)
                {
                    object enumValue = System.Enum.ToObject(enumType, this.FieldValue);
                    return Regex.Replace(this.FormatFieldValue(enumValue), "(\\B[A-Z])", " $1");
                }

                return Regex.Replace(this.FieldValueString, "(\\B[A-Z])", " $1");
            }
        }
    }
}
